package Controllers;

import Connection.IPServer;
import Entities.Phase;
import StateInformation.GameState;
import SwingComponents.ArkanoidPanel;
import Visitor.GraphicPaintVisitor;
import Visitor.IVisitor;
import Visitor.PaintVisitor;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class Renderer extends Thread {

    private ArkanoidPanel arkanoidPanel;
    private Graphics g;
    private Phase scene;
    private boolean stop;
    private Application application;
    private GameState gameState;
    private BufferedImage heartImage;

    public Renderer(Application application, ArkanoidPanel arkanoid, Phase scene, GameState gameState) {
        this.arkanoidPanel = arkanoid;
        this.scene = scene;
        this.application = application;
        this.gameState = gameState;
        stop = false;
    }

    @Override
    public void run() {
        IPServer iPServer = new IPServer(application);
        g = arkanoidPanel.getCanvas().getImage().getGraphics();
        try {
            arkanoidPanel.getTopBoundaryPanel().setImage(ImageIO.read(new URL(iPServer.getAdress() + "/Images/topBoundaryImage.png")));
            arkanoidPanel.getLeftBoundaryPanel().setImage(ImageIO.read(new URL(iPServer.getAdress() + "/Images/leftBoundaryImage.png")));
            arkanoidPanel.getRightBoundaryPanel().setImage(ImageIO.read(new URL(iPServer.getAdress() + "/Images/rightBoundaryImage.png")));
            arkanoidPanel.getDownBoundaryPanel().setImage(ImageIO.read(new URL(iPServer.getAdress() + "/Images/downBoundaryImage.png")));
            heartImage = ImageIO.read(new URL(iPServer.getAdress() + "/Images/heart_icon.png"));
        } catch (Exception e) {
        }
        IVisitor paintVisitor = new GraphicPaintVisitor(application, g);
        while (!stop) {
            try {
                g.fillRect(0, 0, 465, 584);
                synchronized (scene){
                    scene.accept(paintVisitor);
                }

                renderGameState();

                arkanoidPanel.repaint();
                Thread.sleep(5);
            } catch (InterruptedException ex) {
                Logger.getLogger(Renderer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void setTerminar(boolean stop) {
        this.stop = stop;
    }

    private void renderGameState() {
        Graphics scoreGraphics;
        Graphics lifeGraphics;
        Graphics timeGraphics;
        
        /*graphics = arkanoidPanel.getLifePanel().getImage().getGraphics();
        for (int i = 0; i < gameState.getNumberOfLifes(); i++) {
            graphics.drawImage(heartImage, heartImage.getWidth() * i, 0, null);
        }*/

        scoreGraphics = arkanoidPanel.getScorePanel().getImage().getGraphics();
        scoreGraphics.setFont(new Font("TimeRoman", Font.PLAIN, 24));
        scoreGraphics.setColor(Color.red);
        scoreGraphics.fillRect(0, 0, arkanoidPanel.getScorePanel().getWidth(), arkanoidPanel.getScorePanel().getHeight());
        scoreGraphics.setColor(Color.black);
        scoreGraphics.drawRect(0, 0, arkanoidPanel.getScorePanel().getWidth() - 1, arkanoidPanel.getScorePanel().getHeight()-1);
        scoreGraphics.drawString("SCORE", 80, 20);
        scoreGraphics.drawString(gameState.getScore().toString(), 80, 50);
        
        lifeGraphics = arkanoidPanel.getLifePanel().getImage().getGraphics();
        lifeGraphics.setFont(new Font("TimeRoman", Font.PLAIN, 24));
        lifeGraphics.setColor(Color.red);
        lifeGraphics.fillRect(0, 0, arkanoidPanel.getLifePanel().getWidth(), arkanoidPanel.getLifePanel().getHeight());
        lifeGraphics.setColor(Color.BLACK);
        Integer aux = gameState.getNumberOfLifes();
        lifeGraphics.drawRect(0, 0, arkanoidPanel.getScorePanel().getWidth() - 1, arkanoidPanel.getScorePanel().getHeight()-1);
        lifeGraphics.drawString("LIFES", 80, 20);
        lifeGraphics.drawString(aux.toString(), 80, 50);

        timeGraphics = arkanoidPanel.getTimePanel().getImage().getGraphics();
        timeGraphics.setFont(new Font("TimeRoman", Font.PLAIN, 24));
        timeGraphics.setColor(Color.red);
        timeGraphics.fillRect(0, 0, arkanoidPanel.getTimePanel().getWidth(), arkanoidPanel.getTimePanel().getHeight());
        timeGraphics.setColor(Color.BLACK);
        timeGraphics.drawRect(0, 0, arkanoidPanel.getScorePanel().getWidth() - 1, arkanoidPanel.getScorePanel().getHeight()-1);
        timeGraphics.drawString("TIME", 80, 20);
        if (gameState.getPlayedTime().getTime().getSeconds() < 10) {
            timeGraphics.drawString(gameState.getPlayedTime().getTime().getMinutes() + ":0" + gameState.getPlayedTime().getTime().getSeconds(), 80, 50);
        } else {
            timeGraphics.drawString(gameState.getPlayedTime().getTime().getMinutes() + ":" + gameState.getPlayedTime().getTime().getSeconds(), 80, 50);
        }
    }
    
    private void drawNumber (String number, int x, int y){
        //TODO: método para dibujar números con imágenes
    }
}
